# -*- coding: utf-8 -*-
"""
设备型号查询工具
帮助你找到Apple设备的正确型号标识符
"""

import sys
import io
import requests

if sys.platform == 'win32':
    sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

print("=" * 80)
print("Apple设备型号查询工具")
print("=" * 80)

device_name = input("\n请输入设备名称（如: iPhone 15 Pro Max）: ").strip()

if not device_name:
    print("未输入设备名称，退出")
    sys.exit(0)

print(f"\n正在搜索: {device_name}")
print("-" * 80)

try:
    # 使用IPSW.me API搜索设备
    url = "https://api.ipsw.me/v4/devices"
    print(f"查询API: {url}")
    
    response = requests.get(url, timeout=15)
    if response.status_code == 200:
        devices = response.json()
        print(f"[OK] 获取到 {len(devices)} 个设备")
        
        # 搜索匹配的设备
        matches = []
        search_lower = device_name.lower()
        
        for device in devices:
            name = device.get('name', '')
            identifier = device.get('identifier', '')
            
            if search_lower in name.lower():
                matches.append({
                    'name': name,
                    'identifier': identifier,
                    'boardconfig': device.get('boardconfig', 'N/A'),
                    'platform': device.get('platform', 'N/A')
                })
        
        if matches:
            print(f"\n找到 {len(matches)} 个匹配的设备:\n")
            
            for i, device in enumerate(matches, 1):
                print(f"{i}. {device['name']}")
                print(f"   型号标识符: {device['identifier']}")
                print(f"   BoardConfig: {device['boardconfig']}")
                print(f"   平台: {device['platform']}")
                print()
            
            print("=" * 80)
            print("使用说明:")
            print("=" * 80)
            print(f"请在 .env 文件中设置:")
            print(f"DEVICE_MODEL={matches[0]['identifier']}")
            print()
            print(f"示例完整配置:")
            print(f"DEVICE_MODEL={matches[0]['identifier']}")
            print(f"TARGET_VERSION=18.0")
            print(f"CHECK_INTERVAL=1800")
            
        else:
            print(f"\n未找到匹配的设备")
            print(f"\n建议:")
            print(f"1. 检查拼写是否正确")
            print(f"2. 尝试使用英文名称（如: iPhone 15 Pro Max）")
            print(f"3. 访问 https://ipsw.me/product 查看完整设备列表")
            
            # 显示一些常见设备作为示例
            print(f"\n常见设备型号示例:")
            common_devices = [d for d in devices if 'iPhone' in d.get('name', '') or 'iPad' in d.get('name', '')]
            for device in common_devices[:10]:
                name = device.get('name', 'N/A')
                identifier = device.get('identifier', 'N/A')
                print(f"  - {name}: {identifier}")
    else:
        print(f"[失败] HTTP {response.status_code}")
        
except Exception as e:
    print(f"[错误] {e}")
    import traceback
    traceback.print_exc()

print("\n" + "=" * 80)

